package com.hgyc.mom.mes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgyc.mom.common.exception.BizException;
import com.hgyc.mom.common.util.StringUtils;
import com.hgyc.mom.mes.entity.SopTerminal;
import com.hgyc.mom.mes.enums.SopTerminalStatusEnum;
import com.hgyc.mom.mes.mapper.SopTerminalMapper;
import com.hgyc.mom.mes.service.SopTerminalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 显示终端 Service业务层实现
 *
 * @author fwj
 * @since 2025-06-24
 */
@Slf4j
@Service
public class SopTerminalServiceImpl extends ServiceImpl<SopTerminalMapper, SopTerminal> implements SopTerminalService {

    @Override
    public void saveTerminal(SopTerminal sopTerminal) {
        String ip = sopTerminal.getIp();
        SopTerminal existTerminal = getByIp(ip);
        if (existTerminal != null) {
            throw new BizException("IP：["+ip+"]已被使用！");
        }
        save(sopTerminal);
    }

    @Override
    public void updateTerminal(SopTerminal sopTerminal) {
        // TODO 其他逻辑
        updateById(sopTerminal);
    }

    @Override
    public void removeTerminal(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
    }

    @Override
    public SopTerminal getByIp(String ip) {
        LambdaQueryWrapper<SopTerminal> qw = new LambdaQueryWrapper<>();
        qw.eq(StringUtils.isNoneBlank(ip), SopTerminal::getIp, ip);
        return baseMapper.selectOne(qw, false);
    }

    @Override
    public SopTerminal getSopTerminal(Long lineId, Long stationId) {
        LambdaQueryWrapper<SopTerminal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SopTerminal::getLineId, lineId);
        queryWrapper.eq(SopTerminal::getStationId, stationId);
        return baseMapper.selectOne(queryWrapper, false);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<SopTerminal> sopTerminalList, boolean updateSupport) {
        if (sopTerminalList == null || sopTerminalList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + sopTerminalList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + sopTerminalList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("显示终端 导入失败", e);
            throw new BizException("显示终端 导入失败：" + e.getMessage());
        }
    }

    @Override
    public void updateTerminalByIP(String ip, SopTerminalStatusEnum status) {
        LambdaUpdateWrapper<SopTerminal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SopTerminal::getIp, ip);
        updateWrapper.set(SopTerminal::getStatus, status.getValue());
        update(updateWrapper);
    }
}
